/*
 * @lc app=leetcode.cn id=43 lang=java
 *
 * [43] 字符串相乘
 */

// @lc code=start


class Solution {
    public String multiply(String num1, String num2) {
        int len1 = num1.length(), len2 = num2.length();
        int[] A = new int[len1], B = new int[len2];
        int[] res = new int[len1 + len2];

        for(int i = len1 - 1; i >= 0; i--) {
            A[len1 - 1 - i] = num1.charAt(i) - '0';
        }
        for(int i = len2 - 1; i >= 0; i--) {
            B[len2 - 1 - i] = num2.charAt(i) - '0';
        }

        for(int i = 0; i < len1; i++) {
            for(int j = 0; j < len2; j++) {
                res[i + j] += A[i] * B[j];
            }
        }

        int t = 0;
        for(int i = 0; i < res.length; i++) {
            t += res[i];
            res[i] = t % 10;
            t /= 10;
        }

        int k = res.length - 1;
        while(k > 0 && res[k] == 0) {
            k--;
        }

        StringBuilder sb = new StringBuilder();
        for(int i = k; i >= 0; i--) {
            sb.append(res[i]);
        }
        return sb.toString();
    }
}
// @lc code=end

